package shenlong.android.motorkeeper.key.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import shenlong.android.motorkeeper.key.MainActivity;
import shenlong.android.motorkeeper.key.adapter.BTAdapter;
import shenlong.android.motorkeeper.key.common.Constant;
import shenlong.android.motorkeeper.key.common.Constant.BluetoothCommunicationType;
import shenlong.android.motorkeeper.key.customui.ListViewSwipeGesture;
import shenlong.android.motorkeeper.key.dto.BTDevice;
import shenlong.android.motorkeeper.R;
import android.app.Activity;
import android.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class DevicesFragment extends Fragment{
	private ListView lvDevices;
	private List<BTDevice> blueToothDevices;
	public DevicesFragment() {
    }
	public static DevicesFragment newInstance(int sectionNumber) {
		DevicesFragment fragment = new DevicesFragment();
        Bundle args = new Bundle();
        args.putInt(Constant.ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	
        View rootView = inflater.inflate(R.layout.fragment_devices, container, false);
        lvDevices = (ListView)rootView.findViewById(R.id.lvDevices);
        blueToothDevices = new ArrayList<BTDevice>();
        makeSureBluetoothEnable();
        getPariedDevices();
        loadBlueToothDevices();
        return rootView;
    }
    private void makeSureBluetoothEnable()
    {
    	BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    	if (mBluetoothAdapter == null) {
    	    // Device does not support Bluetooth
    	}
    	else if (!mBluetoothAdapter.isEnabled()) {
    	    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    	    startActivityForResult(enableBtIntent, Constant.REQUEST_ENABLE_BT);
    	}
    }
    private void getPariedDevices()
    {    	
    	BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    	Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    	if (pairedDevices.size() > 0) {
	    	for (BluetoothDevice device : pairedDevices) {
		        BTDevice btDevice = new BTDevice(device, BluetoothCommunicationType.PAIRED);
		        blueToothDevices.add(btDevice);
		    }
    	}
    }
    private void loadBlueToothDevices()
    {
    	BTAdapter btAdapter	=	new BTAdapter(getActivity(), blueToothDevices);
		lvDevices.setAdapter(btAdapter);
		final ListViewSwipeGesture touchListener = new ListViewSwipeGesture
														(
															lvDevices, 
															swipeListener, 
															getActivity(),
															getResources().getString(R.string.str_use),
															R.drawable.use
															);
		
		lvDevices.setOnTouchListener(touchListener);
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(Constant.ARG_SECTION_NUMBER));
    }
    ListViewSwipeGesture.TouchCallbacks swipeListener = new ListViewSwipeGesture.TouchCallbacks() {

		@Override
		public void FullSwipeListView(int position) {}

		@Override
		public void HalfSwipeListView(int position) {}

		@Override
		public void LoadDataForScroll(int count) {}

		@Override
		public void onDismiss(ListView listView, int[] reverseSortedPositions) {
			// TODO Auto-generated method stub
			Toast.makeText(getActivity().getApplicationContext(),"Delete", Toast.LENGTH_SHORT).show();
			for(int i:reverseSortedPositions){
				//listdata.remove(i);
				//listAdapter.notifyDataSetChanged();
			}
		}

		@Override
		public void OnClickListView(int position) {}		
	};
}



/*
 if (pairedDevices.size() > 0) {
    	    // Loop through paired devices
    	    for (BluetoothDevice device : pairedDevices) {
    	        // Add the name and address to an array adapter to show in a ListView
    	    	list.add(device.getName() + "\n" + device.getAddress());
    	    }
    	}
    	final ArrayAdapter adapter = new ArrayAdapter(this.getActivity(),android.R.layout.simple_list_item_1, list);
    	lvDevices.setAdapter(adapter);
 * */
